<?php

declare(strict_types=1);

namespace App\Dao;

use App\Model\UserActive;
use Hyperf\Context\ApplicationContext;

class UserActiveDao
{
    /**
     * @var UserActiveDao|mixed
     */
    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = Applicationcontext::getcontainer()->get(self::class);
        }
        return self::$instance;
    }

    public function validateAddActive($data)
    {
        $active_data = UserActive::query()->where('date', date('Y-m-d'))->where('uid', $data['uid'])->where('imei', $data['imei'])->where('ip', $data['ip'])->first();
        if (empty($active_data)) {
            $data = array('date' => date('Y-m-d'), 'ip' => $data['ip'], 'uid' => $data['uid'], 'imei' => $data['imei'], 'channel' => $data['channel'], 'ctime' => time());
            UserActive::query()->insert($data);
        } else {
            UserActive::query()->where('id', $active_data['id'])->update(['ctime' => time()]);
        }
    }
}